<?php

namespace app\admin\model;

use app\common\BaseHelper;

use think\Db;
use think\Exception;
use think\Model;

class Membertype extends Model
{
    protected $autoWriteTimestamp = true;
    //会员开通方式 1-充值开通 2-无条件 默认充值开通
    const openStatus_1 = 1;
    //会员权益 0-享受会员价  1-享受会员折扣
    const isMemBerShop_1 = 1;
    //会员获取积分方式 0-两者都有 1-消费获取积分 2-充值获取积分 默认为消费获取积分
    const memberPointStatus_0 = 0;
    const memberPointStatus_1 = 1;
    const memberPointStatus_2 = 2;
    //会员期限  0-无期限 1-有期限
    const isIndefinite_0 = 0;

    /**
     * @param $tenantId
     * @return \think\Paginator
     * @throws \think\exception\DbException
     * 获取当前商户会员类型列表
     */
    public function getTemantMemberTypeList($tenantId){
        $tenantMemberList = self::where('tenantId',"=",$tenantId)->order('create_time','desc')->paginate(config("paginate['list_rows']"));
        return $tenantMemberList;
    }

    public function addmembertype($postdata){
        Db::startTrans();
        try{
            $MemberruleModel = new Memberrule();
            $Memberruleid = BaseHelper::getUUID();
            $MemberruleModel->id = $Memberruleid;
            $MemberruleModel->openStatus = $postdata['openStatus'];

            // 会员图案
            if($postdata['is_img'] != 1){
                $this->customTypeImg($postdata['selectImg']);
            }
            if($postdata['openStatus'] == self::openStatus_1){
                $MemberruleModel->rechargeOpenMoney = $postdata['rechargeOpenMoney'] * 100;
                if($postdata['returnMoney']){
                    $MemberruleModel->returnMoney = $postdata['returnMoney'] * 100;
                }
                if($postdata['rechargeIntegral']){
                    $MemberruleModel->rechargeIntegral = $postdata['rechargeIntegral'];
                }
            }else{

            }
            $MemberruleModel->isMemBerShop = $postdata['isMemBerShop'];
            if($postdata['isMemBerShop'] == self::isMemBerShop_1){
                $MemberruleModel->memberDiscounts = $postdata['memberDiscounts'];
            }
            if(isset($postdata['tempPointStatus']) && $postdata['tempPointStatus'] == self::memberPointStatus_1){
                $MemberruleModel->memberPointStatus = self::memberPointStatus_1;
                $MemberruleModel->consumeMoney = $postdata['consumeMoney'] * 100;
                $MemberruleModel->consumeIntegral = $postdata['consumeIntegral'];
            }else{

            }
            $MemberruleModel->save();
            $this->id = BaseHelper::getUUID();
            $this->tenantId = $postdata['tenantId'];
            $this->memType = $postdata['memType'];
            $this->img = $postdata['selectImg'];
            $this->openCondition = $postdata['openStatus'];
            $this->isIndefinite = $postdata['isIndefinite'];
            $this->memRuleId = $Memberruleid;
            if($this->isIndefinite == self::isIndefinite_0){
                $this->endTime = 0;
            }else{
                $this->endTime = (int)$postdata['term'] * $this->jisuantime($postdata['timetype']);
            }
            $this->memEquilties = $postdata['isMemBerShop'];
            $this->term = $postdata['term'];
            $this->timetype = $postdata['timetype'];
            $this->save();

            Db::commit();
            return true;
        }catch (Exception $ex){
            Db::rollback();
            return false;
        }
    }

    public function editMembertype($postdata){
        Db::startTrans();
        try{
            $Memberruleid = $postdata['memRuleId'];
            $MemberruleModel = Memberrule::get($Memberruleid);
            $MemberruleModel->openStatus = $postdata['openStatus'];

            // 会员图案
            if($postdata['is_img'] != 1){
                $this->customTypeImg($postdata['selectImg']);
            }
            // 开通条件
            if($postdata['openStatus'] == self::openStatus_1){
                $MemberruleModel->rechargeOpenMoney = $postdata['rechargeOpenMoney'] * 100;
                if($postdata['returnMoney']){
                    $MemberruleModel->returnMoney = $postdata['returnMoney'] * 100;
                }else{
                    $MemberruleModel->returnMoney = '';
                }
                $MemberruleModel->rechargeIntegral = $postdata['rechargeIntegral'];
            }else{
                $MemberruleModel->rechargeOpenMoney = '';
                $MemberruleModel->returnMoney = '';
                $MemberruleModel->rechargeIntegral = '';
            }

            $MemberruleModel->isMemBerShop = $postdata['isMemBerShop'];
            // 会员权益
            if($postdata['isMemBerShop'] == self::isMemBerShop_1){
                $MemberruleModel->memberDiscounts = $postdata['memberDiscounts'];
            }else{
                $MemberruleModel->memberDiscounts = '';
            }
            // 会员积分
            if(isset($postdata['tempPointStatus']) && $postdata['tempPointStatus'] == self::memberPointStatus_1){
                $MemberruleModel->memberPointStatus = self::memberPointStatus_1;
                $MemberruleModel->consumeMoney = $postdata['consumeMoney'] * 100;
                $MemberruleModel->consumeIntegral = $postdata['consumeIntegral'];
            }else{
                $MemberruleModel->consumeMoney = '';
                $MemberruleModel->consumeIntegral = '';
            }

            $MemberruleModel->save();

            $membertype = Membertype::find($postdata['id']);
            $membertype->tenantId = $postdata['tenantId'];
            $membertype->memType = $postdata['memType'];
            $membertype->img = $postdata['selectImg'];
            $membertype->openCondition = $postdata['openStatus'];
            $membertype->isIndefinite = $postdata['isIndefinite'];
            $membertype->memRuleId = $Memberruleid;
            // 会员期限
            if($membertype->isIndefinite == self::isIndefinite_0){
                $membertype->term = '';
                $membertype->endTime = 0;
                $membertype->timetype = '';
            }else{
                $membertype->term = $postdata['term'];
                $membertype->endTime = (string)($postdata['term'] * $this->jisuantime($postdata['timetype']));
                $membertype->timetype = $postdata['timetype'];
            }
            $membertype->memEquilties = $postdata['isMemBerShop'];
            $membertype->save();

            Db::commit();
            return true;
        }catch (Exception $ex){
            Db::rollback();
            return false;
        }
    }

    /**
     * 保存自定义的会员类型图案
     * @param $imgUrl
     */
    public function customTypeImg($imgUrl){
        $data = [
            'id' => BaseHelper::getUUID(),
            'imgUrl' => $imgUrl,
            'type' => 2,
            'tenantId' => session('TENANT_ID')
        ];
        $result = db('Memberdefaultimage')->insert($data);
    }

    /**
     * 计算时间
     * @param $timetype
     * @return float|int
     */
    private function jisuantime($timetype){
        if($timetype == 1){
            $time = 365*24*3600;
        }
        if($timetype == 2){
            $time = 30.5*24*3600;
        }

        return $time;
    }

    /**
     * 会员规则
     * @return \think\model\relation\HasOne
     */
    public function memberrule()
    {
        return $this->hasOne('Memberrule', 'id', 'memRuleId');
    }

    /**
     * 会员
     * @return \think\model\relation\HasOne
     */
    public function member()
    {
        return $this->hasMany('User', 'typeId', 'id')->field('typeId');
    }
}